package com.saihai.jiankang.ufTjglSsrw.controller;

import com.saihai.common.annotation.Log;
import com.saihai.common.constant.JianKangConstant;
import com.saihai.common.core.controller.BaseController;
import com.saihai.common.core.domain.AjaxResult;
import com.saihai.common.core.page.TableDataInfo;
import com.saihai.common.enums.BusinessType;
import com.saihai.common.exception.ServiceException;
import com.saihai.common.utils.MessageUtils;
import com.saihai.common.utils.StringUtils;
import com.saihai.common.utils.objectNullUtils.objectNullUtils;
import com.saihai.common.utils.poi.ExcelUtil;
import com.saihai.jiankang.student.domain.UfXs;
import com.saihai.jiankang.ufBmifwqf.domain.UfBmifwqf;
import com.saihai.jiankang.ufBmifwqf.service.IUfBmifwqfService;
import com.saihai.jiankang.ufDjhfbz.service.IUfDjhfbzService;
import com.saihai.jiankang.ufSsrwLog.domain.UfSsrwLog;
import com.saihai.jiankang.ufSsrwLog.service.IUfSsrwLogService;
import com.saihai.jiankang.ufTjglSsrw.domain.ChineseDescription;
import com.saihai.jiankang.ufTjglSsrw.domain.UfTjglSsrw;
import com.saihai.jiankang.ufTjglSsrw.domain.tijianAccept;
import com.saihai.jiankang.ufTjglSsrw.service.IUfTjglSsrwService;
import com.saihai.jiankang.ufTjjgsfyc.domain.UfTjjgsfyc;
import com.saihai.jiankang.ufTjjgsfyc.service.IUfTjjgsfycService;
import com.saihai.jiankang.ufTjxxxgjlb.domain.UfTjxxxgjlb;
import com.saihai.jiankang.ufTjxxxgjlb.service.IUfTjxxxgjlbService;
import com.saihai.jiankang.ufYsgl.domain.UfYsgl;
import com.saihai.jiankang.ufYsgl.service.IUfYsglService;
import com.saihai.jiankang.zzjg.domain.UfZzjg;
import com.saihai.jiankang.zzjg.service.IUfZzjgService;
import io.netty.util.internal.ObjectUtil;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 体检结果Controller
 *
 * @author hzn
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/jiankang/ufTjglSsrw")
public class UfTjglSsrwController extends BaseController {
    @Autowired
    private IUfTjglSsrwService ufTjglSsrwService;

    @Autowired
    private IUfYsglService ufYsglService;

    @Autowired
    private IUfSsrwLogService ufSsrwLogService;

    @Autowired
    private IUfTjxxxgjlbService ufTjxxxgjlbService;

    @Autowired
    private IUfTjjgsfycService ufTjjgsfycService;

    @Autowired
    private IUfZzjgService ufZzjgService;

    @Autowired
    private IUfBmifwqfService ufBmifwqfService;

    @Autowired
    private IUfDjhfbzService ufDjhfbzService;

    /**
     * 查询体检结果列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UfTjglSsrw ufTjglSsrw) {
        if (StringUtils.isNull(ufTjglSsrw.getXsId())) {
            throw new ServiceException(MessageUtils.message("学生的信息不能为空！"));
        }
        List<UfTjglSsrw> list = ufTjglSsrwService.selectUfTjglSsrwList(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果列表
     */
    @GetMapping("/selectList")
    public TableDataInfo selectList(UfTjglSsrw ufTjglSsrw) {

        if (StringUtils.isNotNull(ufTjglSsrw.getSzxxid())) {
            UfZzjg ufZzjg = ufZzjgService.selectUfZzjgById(ufTjglSsrw.getSzxxid());
            //1市集 2区级
            if (ufZzjg.getLx() != 6 && ufZzjg.getLx() != 3) {
                UfZzjg ufZzjg1 = new UfZzjg();
                ufZzjg1.setLx(6L);
                ufZzjg1.setId(ufTjglSsrw.getSzxxid());
                List<UfZzjg> list1 = ufZzjgService.selectChildDept(ufZzjg1);
                String ids[] = new String[list1.size()];
                for (int i = 0; i < list1.size(); i++) {
                    ids[i] = String.valueOf(list1.get(i).getId());
                }
                ufTjglSsrw.setSzxxid(null);
                ufTjglSsrw.setSzxxids(ids);
            } else if (ufZzjg.getLx() == 6) {
                ufTjglSsrw.setSzbjid(ufTjglSsrw.getSzxxid());
                ufTjglSsrw.setSzxxid(null);
            }
        }
        if (StringUtils.isNotNull(ufTjglSsrw.getRoleType()) ) {
            if (ufTjglSsrw.getRoleType().equals("JZG")) {
                ufTjglSsrw.setZhurwid(ufTjglSsrw.getZirwid());
                ufTjglSsrw.setZirwid(null);
            }
        }
        startPage();
        List<UfTjglSsrw> list = ufTjglSsrwService.selectUfTjglSsrwList(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 家长确认
     */
    @GetMapping("/confirmTjbg/{id}")
    public AjaxResult confirmTjbg(@PathVariable("id") Long id) {
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setJzqr(JianKangConstant.JZQR);
        ufTjglSsrw.setId(id);
        return toAjax(ufTjglSsrwService.updateUfTjglSsrw(ufTjglSsrw));
    }

    /**
     * 体检结果录入
     */
    @PutMapping("/physicalExaminationInto")
    @Transactional
    public AjaxResult physicalExaminationInto(@RequestBody UfTjglSsrw ufTjglSsrw) {
        if (StringUtils.isNull(ufTjglSsrw)) {
            return AjaxResult.error("数据不能为空！");
        }
        if (StringUtils.isBlank(ufTjglSsrw.getJbs())) {
            ufTjglSsrw.setJbs(null);
        }
        //是否第一次修改
        Long isUpdate = 0L;
        UfTjglSsrw ufTjglSsrw_select = ufTjglSsrwService.selectUfTjglSsrwById(ufTjglSsrw.getId());
        //写入体检日期
        LocalDate currentDate = LocalDate.now();
        ufTjglSsrw.setModedatamodifydatetime(currentDate.toString());
        //第一次修改体检信息
        if (ufTjglSsrw_select.getSign() == 0) {
            //写入季节
            ufTjglSsrw.setJj(getSeason());
            //标记证明已写入
            ufTjglSsrw.setSign(1L);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String S = currentDate.format(formatter);
            ufTjglSsrw.setTjsj(S);
        }
        /************************************* 内科  *************************************/

        /**
         * 心大类
         */

        String XJG = "";
        //心率
        Long XL = null;
        //心律
        Long xlv = null;
        //心脏
        Long xz = null;
        if (StringUtils.isNotNull(ufTjglSsrw.getXl())) {
            XL = ufTjglSsrw.getXl();
            //判断心率
            if (ufTjglSsrw.getXl() != 0 && ufTjglSsrw.getXl() != 3) {
                if (ufTjglSsrw.getXl() == 1) {
                    XJG += "心动过速,";
                }
                if (ufTjglSsrw.getXl() == 2) {
                    XJG += "心动过缓,";
                }
            }
        } else {
            if (StringUtils.isNotNull(ufTjglSsrw_select.getXl())) {
                XL = ufTjglSsrw_select.getXl();
                //判断心率
                if (ufTjglSsrw_select.getXl() != 0 && ufTjglSsrw_select.getXl() != 3) {
                    if (ufTjglSsrw_select.getXl() == 1) {
                        XJG += "心动过速,";
                    }
                    if (ufTjglSsrw_select.getXl() == 2) {
                        XJG += "心动过缓,";
                    }
                }
            }
        }

        if (StringUtils.isNotNull(ufTjglSsrw.getXlv())) {
            xlv = ufTjglSsrw.getXlv();
            //判断心律
            if (ufTjglSsrw.getXlv() != 0 && ufTjglSsrw.getXlv() != 3) {
                if (ufTjglSsrw.getXlv() == 1) {
                    XJG += "心脏早搏,";
                }
                if (ufTjglSsrw.getXlv() == 2) {
                    XJG += "心律不齐,";
                }
            }
        } else {
            if (StringUtils.isNotNull(ufTjglSsrw_select.getXlv())) {
                xlv = ufTjglSsrw_select.getXlv();
                //判断心律
                if (ufTjglSsrw_select.getXlv() != 0 && ufTjglSsrw_select.getXlv() != 3) {
                    if (ufTjglSsrw_select.getXlv() == 1) {
                        XJG += "心脏早搏,";
                    }
                    if (ufTjglSsrw_select.getXlv() == 2) {
                        XJG += "心律不齐,";
                    }
                }
            }
        }

        if (StringUtils.isNotNull(ufTjglSsrw.getXz())) {
            xz = ufTjglSsrw.getXz();
            //判断心脏
            if (ufTjglSsrw.getXz() != 0 && ufTjglSsrw.getXz() != 3) {
                if (ufTjglSsrw.getXz() == 1) {
                    XJG += "心脏有杂音,";
                }
                if (ufTjglSsrw.getXz() == 2) {
                    XJG += "心音改变,";
                }
            }
        } else {
            if (StringUtils.isNotNull(ufTjglSsrw_select.getXz())) {
                xz = ufTjglSsrw_select.getXz();
                //判断心脏
                if (ufTjglSsrw_select.getXz() != 0 && ufTjglSsrw_select.getXz() != 3) {
                    if (ufTjglSsrw_select.getXz() == 1) {
                        XJG += "心脏有杂音,";
                    }
                    if (ufTjglSsrw_select.getXz() == 2) {
                        XJG += "心音改变,";
                    }
                }
            }
        }

        String xqt = null;
        if (StringUtils.isNotNull(ufTjglSsrw.getXqt())) {
            xqt = ufTjglSsrw.getXqt();
            XJG += ufTjglSsrw.getXqt();
        } else {
            if (StringUtils.isNotNull(ufTjglSsrw_select.getXqt())) {
                xqt = ufTjglSsrw_select.getXqt();
                XJG += ufTjglSsrw_select.getXqt() + ",";
            }
        }
        if (StringUtils.isNotBlank(XJG)) {
            ufTjglSsrw.setxJg(XJG.substring(0, XJG.length() - 1));
        }

        if (StringUtils.isNotNull(XL) && StringUtils.isNotNull(xlv)) {
            if (XL == 0 && xlv == 0 && xz == 0 && StringUtils.isBlank(xqt)) {
                ufTjglSsrw.setxJg("无异常");
            }
        }

        /**************** 内科其他  ****************/
        if (StringUtils.isNotNull(ufTjglSsrw.getQt())) {
            if (ufTjglSsrw.getQt() == 0) {
                ufTjglSsrw.setNkqtJg("无");
            }
            if (ufTjglSsrw.getQt() == 1) {
                ufTjglSsrw.setNkqtJg("有");
                if (StringUtils.isNotBlank(ufTjglSsrw.getQtwb())) {
                    ufTjglSsrw.setNkqtJg("有（" + ufTjglSsrw.getQtwb() + ")");
                }

            }
        }

        /*********************  肺  ****************************************/
        if (StringUtils.isNotNull(ufTjglSsrw.getFei())) {
            String FJG = "";
            if (ufTjglSsrw.getFei() == 0) {
                FJG = "无杂音";
            } else {
                FJG="有杂音";
                if (StringUtils.isNotNull(ufTjglSsrw.getFbyzy())) {
                    if (ufTjglSsrw.getFbyzy() == 0) {
                        FJG = "有杂音(干啰音)";
                    } else {
                        FJG = "有杂音(湿啰音)";
                    }
                }
            }
            ufTjglSsrw.setfJg(FJG);
        }

        /*********************  肝  ****************************************/
        if (StringUtils.isNotNull(ufTjglSsrw.getGan())) {
            String GJG = "";
            if (ufTjglSsrw.getGan() == 0) {
                GJG = "无异常";
            } else {
                GJG = "有异常";
                if (StringUtils.isNotBlank(ufTjglSsrw.getGwb())) {
                    GJG = "有异常(" + ufTjglSsrw.getGwb() + ")";
                }
            }
            ufTjglSsrw.setgJg(GJG);
        }

        /*********************  脾  ****************************************/
        if (StringUtils.isNotNull(ufTjglSsrw.getPi())) {
            String PJG = "";
            if (ufTjglSsrw.getPi() == 0) {
                PJG = "无异常";
            } else {
                PJG = "有异常";
                if (StringUtils.isNotBlank(ufTjglSsrw.getPwb())) {
                    PJG = "有异常(" + ufTjglSsrw.getPwb() + ")";
                }
            }
            ufTjglSsrw.setpJg(PJG);
        }

        /*********************  腹部  ****************************************/
        if (StringUtils.isNotNull(ufTjglSsrw.getFb())) {
            String FBJG = "";
            if (ufTjglSsrw.getFb() == 0) {
                FBJG = "无异常";
            }
            if (ufTjglSsrw.getFb() == 1) {
                FBJG = "腹部有包块";
                if (StringUtils.isNotBlank(ufTjglSsrw.getFbwb())) {
                    FBJG = "腹部有包块(" + ufTjglSsrw.getFbwb() + ")";
                }
            }
            if (ufTjglSsrw.getFb() == 2) {
                FBJG = "腹部有积水";
            }
            ufTjglSsrw.setFbJg(FBJG);
        }

        /**************** 外科  ********************/

        /**************** 头部  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getTb())) {
            String TBJG = "";
            if (ufTjglSsrw.getTb() == 0) {
                TBJG = "无异常";
            }
            if (ufTjglSsrw.getTb() == 1) {
                TBJG = "附耳";
            }
            if (ufTjglSsrw.getTb() == 2) {
                TBJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getTbwb())) {
                    TBJG = "其他(" + ufTjglSsrw.getTbwb() + ")";
                }
            }
            ufTjglSsrw.setTbjJg(TBJG);
        }

        /**************** 颈部  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getJb())) {
            String JBJG = "";
            if (ufTjglSsrw.getJb() == 0) {
                JBJG = "无异常";
            }
            if (ufTjglSsrw.getJb() == 1) {
                JBJG = "斜颈";
            }
            if (ufTjglSsrw.getJb() == 2) {
                JBJG = "甲状腺肿大";
            }
            if (ufTjglSsrw.getJb() == 3) {
                JBJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getJbwb())) {
                    JBJG = "其他(" + ufTjglSsrw.getTbwb() + ")";
                }
            }
            ufTjglSsrw.setJbJg(JBJG);
        }

        /**************** 胸部  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getXiongb())) {
            String XBJG = "";
            if (ufTjglSsrw.getXiongb() == 0) {
                XBJG = "无异常";
            }
            if (ufTjglSsrw.getXiongb() == 1) {
                XBJG = "漏斗胸";
            }
            if (ufTjglSsrw.getXiongb() == 2) {
                XBJG = "鸡胸";

            }
            if (ufTjglSsrw.getXiongb() == 3) {
                XBJG = "胸廓不对称";

            }
            if (ufTjglSsrw.getXiongb() == 4) {
                XBJG = "肋缘外翻";

            }
            if (ufTjglSsrw.getXiongb() == 5) {
                XBJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getXbwb())) {
                    XBJG = "其他(" + ufTjglSsrw.getXbwb() + ")";
                }
            }
            ufTjglSsrw.setXbJg(XBJG);
        }
        /**************** 脊柱  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getJz())) {
            String JZJG = "";
            if (ufTjglSsrw.getJz() == 0) {
                JZJG = "无异常";
            }
            if (ufTjglSsrw.getJz() == 1) {
                JZJG = "脊柱左侧弯";
                if (StringUtils.isNotNull(ufTjglSsrw.getJzwb())) {
                    JZJG = "脊柱左侧弯(" + ufTjglSsrw.getJzwb() + ")";
                }
            }
            if (ufTjglSsrw.getJz() == 2) {
                JZJG = "脊柱右侧弯";
                if (StringUtils.isNotNull(ufTjglSsrw.getJzwb())) {
                    JZJG = "脊柱右侧弯(" + ufTjglSsrw.getJzwb() + ")";
                }
            }
            if (ufTjglSsrw.getJz() == 3) {
                JZJG = "脊柱其他异常";
                if (StringUtils.isNotNull(ufTjglSsrw.getJzwb())) {
                    JZJG = "其他(" + ufTjglSsrw.getJzwb() + ")";
                }
            }
            ufTjglSsrw.setJzJg(JZJG);
        }

        /**************** 四肢  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getSz())) {
            String SZJG = "";
            if (ufTjglSsrw.getSz() == 0) {
                SZJG = "无异常";
            }
            if (ufTjglSsrw.getJz() == 1) {
                SZJG = "扭伤";
                if (StringUtils.isNotNull(ufTjglSsrw.getJzwb())) {
                    SZJG = "扭伤(" + ufTjglSsrw.getJzwb() + ")";
                }
            }
            if (ufTjglSsrw.getJz() == 2) {
                SZJG = "长短不一";
            }
            if (ufTjglSsrw.getJz() == 3) {
                SZJG = "四肢不协调";
            }
            if (ufTjglSsrw.getJz() == 4) {
                SZJG = "肌力差";
            }
            if (ufTjglSsrw.getJz() == 5) {
                SZJG = "爪形手";
            }
            if (ufTjglSsrw.getJz() == 6) {
                SZJG = "骨折";
                if (StringUtils.isNotNull(ufTjglSsrw.getSzwb())) {
                    SZJG = "骨折(" + ufTjglSsrw.getSzwb() + ")";
                }
            }
            if (ufTjglSsrw.getJz() == 7) {
                SZJG = "指缺失";
            }
            if (ufTjglSsrw.getJz() == 8) {
                SZJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getSzwb())) {
                    SZJG = "其他(" + ufTjglSsrw.getSzwb() + ")";
                }
            }
            ufTjglSsrw.setSzJg(SZJG);
        }

        /**************** 皮肤  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getPf())) {
            String PFJG = "";
            if (ufTjglSsrw.getPf() == 0) {
                PFJG = "无异常";
            }
            if (ufTjglSsrw.getPf() == 1) {
                PFJG = "皮疹";
            }
            if (ufTjglSsrw.getPf() == 2) {
                PFJG = "皮肤干燥";
            }
            if (ufTjglSsrw.getPf() == 3) {
                PFJG = "黑棘皮病";
            }
            if (ufTjglSsrw.getPf() == 4) {
                PFJG = "胎记 ";
            }
            if (ufTjglSsrw.getPf() == 5) {
                PFJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getPfwb())) {
                    PFJG = "其他(" + ufTjglSsrw.getPfwb() + ")";
                }
            }
            ufTjglSsrw.setPfJg(PFJG);
        }

        /**************** 淋巴  ********************/
        if (StringUtils.isNotNull(ufTjglSsrw.getLb())) {
            String LBJG = "";
            if (ufTjglSsrw.getLb() == 0) {
                LBJG = "无异常";
            }
            if (ufTjglSsrw.getLb() == 1) {
                LBJG = "双颌下淋巴结肿大";
            }
            if (ufTjglSsrw.getLb() == 2) {
                LBJG = "左颌下淋巴结肿大";
            }
            if (ufTjglSsrw.getLb() == 3) {
                LBJG = "右颌下淋巴结肿大";
            }
            if (ufTjglSsrw.getLb() == 4) {
                LBJG = "颈前淋巴结肿大 ";
            }
            if (ufTjglSsrw.getLb() == 5) {
                LBJG = "其他";
                if (StringUtils.isNotNull(ufTjglSsrw.getLbwb())) {
                    LBJG = "其他(" + ufTjglSsrw.getLbwb() + ")";
                }
            }
            ufTjglSsrw.setLbjJg(LBJG);
        }

        if (StringUtils.isNotNull(ufTjglSsrw.getQtwk())) {
            if (ufTjglSsrw.getQtwk() == 0) {
                ufTjglSsrw.setWkqtjJg("无");
            }
            if (ufTjglSsrw.getQtwk() == 1) {
                ufTjglSsrw.setWkqtjJg("有");
                if (StringUtils.isNotBlank(ufTjglSsrw.getQtwbwk())) {
                    ufTjglSsrw.setWkqtjJg("有（" + ufTjglSsrw.getQtwbwk() + ")");
                }

            }
        }
        //判断牙周
        if (StringUtils.isNotNull(ufTjglSsrw.getYz())) {
            if (ufTjglSsrw.getYz() == 0) {
                ufTjglSsrw.setYzwb("");
            }
        }

        //形态指标  BMI 判断肥胖

        if (StringUtils.isNotNull(ufTjglSsrw.getBmi())) {
            UfBmifwqf ufBmifwqf = new UfBmifwqf();
            ufBmifwqf.setZczd(ufTjglSsrw.getBmi());
            ufBmifwqf.setZczg(ufTjglSsrw.getBmi());
            ufBmifwqf.setXb(ufTjglSsrw_select.getXb());
            ufBmifwqf.setNj(ufTjglSsrw_select.getNjpx());
            List<UfBmifwqf> listBmi = ufBmifwqfService.selectUfBmifwqfList_zc(ufBmifwqf);
            if (listBmi.size() > 0) {
                //正常
                ufTjglSsrw.setBmijg(0L);
            }else {
                UfBmifwqf ufBmifwqf1 = new UfBmifwqf();
                ufBmifwqf1.setYybl(ufTjglSsrw.getBmi());
                ufBmifwqf1.setXb(ufTjglSsrw_select.getXb());
                ufBmifwqf1.setNj(ufTjglSsrw_select.getNjpx());
                List<UfBmifwqf> listBmi1 = ufBmifwqfService.selectUfBmifwqfList_zc(ufBmifwqf1);
                if (listBmi1.size()> 0) {
                    //营养不良
                    ufTjglSsrw.setBmijg(1L);
                }else {
                    UfBmifwqf ufBmifwqf2 = new UfBmifwqf();
                    ufBmifwqf2.setCzzg(ufTjglSsrw.getBmi());
                    ufBmifwqf2.setCzzd(ufTjglSsrw.getBmi());
                    ufBmifwqf2.setXb(ufTjglSsrw_select.getXb());
                    ufBmifwqf2.setNj(ufTjglSsrw_select.getNjpx());
                    List<UfBmifwqf> listBmi2 = ufBmifwqfService.selectUfBmifwqfList_zc(ufBmifwqf2);
                    if (listBmi2.size() > 0) {
                        //超重
                        ufTjglSsrw.setBmijg(2L);
                    }else {
                        UfBmifwqf ufBmifwqf3 = new UfBmifwqf();
                        ufBmifwqf3.setXb(ufTjglSsrw_select.getXb());
                        ufBmifwqf3.setNj(ufTjglSsrw_select.getNjpx());
                        ufBmifwqf3.setFp(ufTjglSsrw.getBmi());
                        List<UfBmifwqf> listBmi3 = ufBmifwqfService.selectUfBmifwqfList_zc(ufBmifwqf3);
                        if (listBmi3.size() > 0) {
                            //肥胖
                            ufTjglSsrw.setBmijg(3L);
                        }
                    }
                }
            }

        }

        //身高等级
        if (StringUtils.isNotNull(ufTjglSsrw.getSg())) {
            int intNum = ufDjhfbzService.yzSg(ufTjglSsrw.getSg(),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setSgdj((long)intNum);
        }

        //体重
        if (StringUtils.isNotNull(ufTjglSsrw.getTz())) {
            int intNum = ufDjhfbzService.yzTz(ufTjglSsrw.getTz(),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setTzdj((long)intNum);
        }

        //肺活量
        if (StringUtils.isNotNull(ufTjglSsrw.getFhl())) {
            int intNum = ufDjhfbzService.yzFhl(new BigDecimal(ufTjglSsrw.getFhl()),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setFhldj((long)intNum);
        }


        //szy舒张压
        if (StringUtils.isNotNull(ufTjglSsrw.getSzy())) {
            int intNum = ufDjhfbzService.yzSzy(new BigDecimal(ufTjglSsrw.getSzy()),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setSzydj((long)intNum);
        }

        //ssy收缩压
        if (StringUtils.isNotNull(ufTjglSsrw.getSsy())) {
            int intNum = ufDjhfbzService.yzSsy(new BigDecimal(ufTjglSsrw.getSsy()),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setSsydj((long)intNum);
        }

        //眼科
        if (StringUtils.isNotNull(ufTjglSsrw.getZy()) && StringUtils.isNotNull(ufTjglSsrw.getYy())) {
            BigDecimal smallerValue;
            if (ufTjglSsrw.getZy().compareTo(ufTjglSsrw.getYy()) < 0) {
                smallerValue = ufTjglSsrw.getZy();
            } else {
                smallerValue = ufTjglSsrw.getYy();
            }
            int intNum = ufDjhfbzService.yzSl(smallerValue.setScale(1, BigDecimal.ROUND_HALF_UP),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setJszt((long)intNum);
        }else if (StringUtils.isNotNull(ufTjglSsrw.getZy()) && StringUtils.isNull(ufTjglSsrw.getYy())) {
            BigDecimal smallerValue;
            if (ufTjglSsrw.getZy().compareTo(ufTjglSsrw_select.getYy()) < 0) {
                smallerValue = ufTjglSsrw.getZy();
            } else {
                smallerValue = ufTjglSsrw_select.getYy();
            }
            int intNum = ufDjhfbzService.yzSl(smallerValue.setScale(1, BigDecimal.ROUND_HALF_UP),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setJszt((long)intNum);
        }else if (StringUtils.isNotNull(ufTjglSsrw.getYy()) && StringUtils.isNull(ufTjglSsrw.getZy())) {
            BigDecimal smallerValue;
            if (ufTjglSsrw_select.getZy().compareTo(ufTjglSsrw.getYy()) < 0) {
                smallerValue = ufTjglSsrw_select.getZy();
            } else {
                smallerValue = ufTjglSsrw_select.getYy();
            }
            int intNum = ufDjhfbzService.yzSl(smallerValue.setScale(1, BigDecimal.ROUND_HALF_UP),ufTjglSsrw_select.getXb(),ufTjglSsrw_select.getNl());
            ufTjglSsrw.setJszt((long)intNum);
        }

        Map<String, Object> oldMap = toMap(ufTjglSsrw_select);
        Set<String> commonKeys = new HashSet<>(oldMap.keySet());
        Map map = objectNullUtils.getNonNullFieldNames(ufTjglSsrw);
        map.remove("serialVersionUID");
        map.remove("modedatamodifydatetime");
        map.remove("id");
        map.remove("doctorId");
        commonKeys.retainAll(map.keySet());
        String[] strArr = new String[commonKeys.size()];
        commonKeys.toArray(strArr);
        System.err.println(strArr);
        String A = null;
        String B = null;
        for (String index : strArr) {
            System.err.println(index);
            if (StringUtils.isNotNull(oldMap.get(index))) {
                A = oldMap.get(index).toString();
            }
            if (StringUtils.isNotNull(map.get(index))) {
                B = map.get(index).toString();
            }
            if (StringUtils.isNotNull(A)) {
                if (A.contains(".")) {
                    A = A.replaceAll("0*$", "").replaceAll("\\.$", "");
                }
            }
            if (StringUtils.isNotNull(B)) {
                if (B.contains(".")) {
                    B = B.replaceAll("0*$", "").replaceAll("\\.$", "");
                }
            }

            if (A != null && !A.equals("9999") && !A.equals(B)) {
                UfTjxxxgjlb ufTjxxxgjlb = new UfTjxxxgjlb();
                ufTjxxxgjlb.setGlxsrwid(ufTjglSsrw.getId());
                ufTjxxxgjlb.setXgzdmc(index);
                ufTjxxxgjlb.setXgqz(A);
                ufTjxxxgjlb.setXghz(B);
                ufTjxxxgjlbService.insertUfTjxxxgjlb(ufTjxxxgjlb);
                isUpdate = 1L;
                System.err.println("修改");
            } else {
                System.err.println("新增");
            }

        }
        //体检日志
        UfSsrwLog ufSsrwLog = new UfSsrwLog();
        //放入医生id
        ufSsrwLog.setYsId(ufTjglSsrw.getDoctorId());
        ufSsrwLog.setXxId(ufTjglSsrw_select.getSzxxid());
        ufSsrwLog.setXsId(ufTjglSsrw_select.getXsId());
        ufSsrwLog.setRwId(ufTjglSsrw_select.getZirwid());
        ufSsrwLog.setXsName(ufTjglSsrw_select.getXm());
        ufSsrwLog.setCreateTime(new Date());
        ufSsrwLog.setProjectJson(map.toString());
        ufSsrwLog.setIsUpdate(isUpdate);
        ufSsrwLog.setSsrwId(ufTjglSsrw_select.getId());
        ufSsrwLogService.insertUfSsrwLog(ufSsrwLog);
        int i = ufTjglSsrwService.updateUfTjglSsrw(ufTjglSsrw);
        UfTjglSsrw ufTjglSsrw_select1 = ufTjglSsrwService.selectUfTjglSsrwById(ufTjglSsrw.getId());
        Map<String, String> map1 = findFieldWithValue(ufTjglSsrw_select1, 9999);
        UfTjglSsrw ufTjglSsrw1 = new UfTjglSsrw();
        if (map1.size() == 0) {
            ufTjglSsrw1.setId(ufTjglSsrw.getId());
            ufTjglSsrw1.setZt(1L);
        } else {
            ufTjglSsrw1.setId(ufTjglSsrw.getId());
            ufTjglSsrw1.setZt(0L);
        }
        //写入异常值
        judgeAbnormal(ufTjglSsrw_select1);
        UfTjjgsfyc tjjgsfyc = ufTjjgsfycService.selectUfTjjgsfycById(ufTjglSsrw_select1.getId());
        Map<String, String> map2 = findFieldWithValue1(tjjgsfyc, 1);
        map2.remove("serialVersionUID");
        map2.remove("id");
        map2.remove("formmodeid");
        map2.remove("szzrw");
        map2.remove("szslrw");
        map2.remove("xs");
        map2.remove("zrwid");
        map2.remove("xstjbid");
        map2.remove("modedatamodifydatetime");
        if (map2.size() == 0) {
            ufTjglSsrw1.setId(ufTjglSsrw.getId());
            ufTjglSsrw1.setTjjg(0L);
        } else {
            ufTjglSsrw1.setId(ufTjglSsrw.getId());
            ufTjglSsrw1.setTjjg(1L);
        }
        //修改体检
        int ii = ufTjglSsrwService.updateUfTjglSsrw(ufTjglSsrw1);
        if (ii > 0) {
            UfTjglSsrw ufTjglSsrw2 =ufTjglSsrwService.selectUfTjglSsrwById_yk(ufTjglSsrw1.getId());
            if (StringUtils.isNotNull(ufTjglSsrw2)) {
                Map<String,Long> intNum = ufDjhfbzService.yzYsl(ufTjglSsrw2.getZy(),ufTjglSsrw2.getZyqj(),ufTjglSsrw2.getYy(),ufTjglSsrw2.getYyqj(),ufTjglSsrw2.getZyzj(),ufTjglSsrw2.getYyzj());
                UfTjglSsrw ufTjglSsrw3 = new UfTjglSsrw();
                ufTjglSsrw3.setId(ufTjglSsrw2.getId());
                ufTjglSsrw3.setYsztyy(intNum.get("yy"));
                ufTjglSsrw3.setYszt(intNum.get("zy"));
                return toAjax(ufTjglSsrwService.updateUfTjglSsrw(ufTjglSsrw3));
            }
            return toAjax(ii);
        }else {
            return toAjax(ii);
        }

    }


    /**
     * 写入体检异常值
     *
     * @param ufTjglSsrw
     */
    public void judgeAbnormal(UfTjglSsrw ufTjglSsrw) {
        UfTjjgsfyc ufTjjgsfyc = new UfTjjgsfyc();
        ufTjjgsfyc.setXstjbid(ufTjglSsrw.getId());
        //心
        if (StringUtils.isNotBlank(ufTjglSsrw.getxJg()) && !ufTjglSsrw.getxJg().equals("9999")) {
            if (ufTjglSsrw.getXl() == 0 && ufTjglSsrw.getXlv() == 0 && ufTjglSsrw.getXz() == 0 && StringUtils.isBlank(ufTjglSsrw.getXqt())) {
                ufTjjgsfyc.setXin(0L);
            } else {
                ufTjjgsfyc.setXin(1L);
            }
        }
        //肺
        if (StringUtils.isNotNull(ufTjglSsrw.getFei())) {
            ufTjjgsfyc.setFei(ufTjglSsrw.getFei());
        }
        //肝
        if (StringUtils.isNotNull(ufTjglSsrw.getGan())) {
            ufTjjgsfyc.setGan(ufTjglSsrw.getGan());
        }
        //脾
        if (StringUtils.isNotNull(ufTjglSsrw.getPi())) {
            ufTjjgsfyc.setPi(ufTjglSsrw.getPi());
        }
        //腹部
        if (StringUtils.isNotNull(ufTjglSsrw.getFb())) {
            if (ufTjglSsrw.getFb() == 0) {
                ufTjjgsfyc.setFb(0L);
            } else {
                ufTjjgsfyc.setFb(1L);
            }
        }
        //内科其他
        if (StringUtils.isNotNull(ufTjglSsrw.getQt())) {
            ufTjjgsfyc.setNkqt(ufTjglSsrw.getQt());
        }
        //头部
        if (StringUtils.isNotNull(ufTjglSsrw.getTb())) {
            if (ufTjglSsrw.getTb() == 0) {
                ufTjjgsfyc.setTb(0L);
            } else {
                ufTjjgsfyc.setTb(1L);
            }
        }
        //颈部
        if (StringUtils.isNotNull(ufTjglSsrw.getJb())) {
            if (ufTjglSsrw.getJb() == 0) {
                ufTjjgsfyc.setJb(0L);
            } else {
                ufTjjgsfyc.setJb(1L);
            }
        }

        //胸部
        if (StringUtils.isNotNull(ufTjglSsrw.getXiongb())) {
            if (ufTjglSsrw.getXiongb() == 0) {
                ufTjjgsfyc.setXb(0L);
            } else {
                ufTjjgsfyc.setXb(1L);
            }
        }

        //脊柱
        if (StringUtils.isNotNull(ufTjglSsrw.getJz())) {
            if (ufTjglSsrw.getJz() == 0) {
                ufTjjgsfyc.setJz(0L);
            } else {
                ufTjjgsfyc.setJz(1L);
            }
        }
        //四肢
        if (StringUtils.isNotNull(ufTjglSsrw.getSz())) {
            if (ufTjglSsrw.getSz() == 0) {
                ufTjjgsfyc.setSz(0L);
            } else {
                ufTjjgsfyc.setSz(1L);
            }
        }

        //皮肤
        if (StringUtils.isNotNull(ufTjglSsrw.getPf())) {
            if (ufTjglSsrw.getPf() == 0) {
                ufTjjgsfyc.setPf(0L);
            } else {
                ufTjjgsfyc.setPf(1L);
            }
        }

        //淋巴结
        if (StringUtils.isNotNull(ufTjglSsrw.getLb())) {
            if (ufTjglSsrw.getLb() == 0) {
                ufTjjgsfyc.setLbj(0L);
            } else {
                ufTjjgsfyc.setLbj(1L);
            }
        }

        //其他外科
        if (StringUtils.isNotNull(ufTjglSsrw.getQtwk())) {
            if (ufTjglSsrw.getQtwk() == 0) {
                ufTjjgsfyc.setWkqt(0L);
            } else {
                ufTjjgsfyc.setWkqt(1L);
            }
        }

        //沙眼
        if (StringUtils.isNotNull(ufTjglSsrw.getSyyy()) && StringUtils.isNotNull(ufTjglSsrw.getSyzy())) {
            if (ufTjglSsrw.getSyyy() == 0 && ufTjglSsrw.getSyzy() == 0) {
                ufTjjgsfyc.setSy(0L);
            } else {
                ufTjjgsfyc.setSy(1L);
            }
        }

        //结膜炎
        if (StringUtils.isNotNull(ufTjglSsrw.getJmyyy()) && StringUtils.isNotNull(ufTjglSsrw.getJmyzy())) {
            if (ufTjglSsrw.getJmyyy() == 0 && ufTjglSsrw.getJmyzy() == 0) {
                ufTjjgsfyc.setJmy(0L);
            } else {
                ufTjjgsfyc.setJmy(1L);
            }
        }

        //麦粒肿
        if (StringUtils.isNotNull(ufTjglSsrw.getMlzyy()) && StringUtils.isNotNull(ufTjglSsrw.getMlzzy())) {
            if (ufTjglSsrw.getMlzyy() == 0 && ufTjglSsrw.getMlzzy() == 0) {
                ufTjjgsfyc.setJmy(0L);
            } else {
                ufTjjgsfyc.setJmy(1L);
            }
        }

        //牙周
        if (StringUtils.isNotNull(ufTjglSsrw.getYz()) && ufTjglSsrw.getYz() != 9999) {
            if (ufTjglSsrw.getYz() == 0) {
                ufTjjgsfyc.setYz(0L);
            } else {
                ufTjjgsfyc.setYz(1L);
            }
        }
        //龋齿
        if (StringUtils.isNotNull(ufTjglSsrw.getQc()) && ufTjglSsrw.getQc() != 9999) {
            if (ufTjglSsrw.getQc() == 0) {
                ufTjjgsfyc.setYc(0L);
            } else {
                ufTjjgsfyc.setYc(1L);
            }
        }
        //写入体检日期
        LocalDate currentDate = LocalDate.now();
        ufTjjgsfyc.setModedatamodifydatetime(currentDate.toString());
        ufTjjgsfycService.updateUfTjjgsfycRwid(ufTjjgsfyc);
    }

    //对象转换为map
    public static Map<String, Object> toMap(Object entity) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = entity.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true); // 使得私有字段也可以访问
            try {
                map.put(field.getName(), field.get(entity));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static String getSeason() {
        LocalDate currentDate = LocalDate.now();
        Month month = currentDate.getMonth();

        switch (month) {
            case MARCH:
            case APRIL:
            case MAY:
                return "春季";
            case JUNE:
            case JULY:
            case AUGUST:
                return "夏季";
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
                return "秋季";
            case DECEMBER:
            case JANUARY:
            case FEBRUARY:
                return "冬季";
            default:
                return "未知季节";
        }
    }


    /**
     * 通过名称查询学生体检列表
     */
    @PostMapping("/selectStudentInfo")
    public TableDataInfo selectStudentInfo(@RequestBody tijianAccept tijianAccept) {
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setZirwid(tijianAccept.getZirwid());
        ufTjglSsrw.setSzxxid(tijianAccept.getSchoolId());
        ufTjglSsrw.setXm(tijianAccept.getNameCode());
        List<UfTjglSsrw> list = ufTjglSsrwService.selectStudentInfoList(ufTjglSsrw);
        return getDataTable(list);
    }


    /**
     * 通过二维码获取学生体检信息
     */
    @PostMapping("/getStudentQRcodeInfo")
    public AjaxResult studentQRcode(@RequestBody tijianAccept tijianAccept) {
        UfYsgl ufYsgl = ufYsglService.selectUfYsglById(tijianAccept.getDoctorId());
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setXsId(tijianAccept.getStudentId());
        ufTjglSsrw.setZirwid(tijianAccept.getZirwid());
        ufTjglSsrw.setSzxxid(tijianAccept.getSchoolId());
        UfTjglSsrw ufTjglSsrw1 = ufTjglSsrwService.selectUfTjglSsrwInfo(ufTjglSsrw);
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNull(ufTjglSsrw1)) {
            return AjaxResult.error("未查找到该学生体检信息！");
        } else {
            map.put("doctor", ufYsgl);
            map.put("studentInfo", ufTjglSsrw1);
            Map<String, String> map1 = findFieldWithValue(ufTjglSsrw1, 9999);
            map.put("noTj", map1);
            return AjaxResult.success(map);
        }
    }


    /**
     * 确认医生扫码 通过二维码获取学生体检信息
     */
    @PostMapping("/getStudentQRcodeQzYsInfo")
    public AjaxResult getStudentQRcodeQzYsInfo(@RequestBody tijianAccept tijianAccept) {
        UfYsgl ufYsgl = ufYsglService.selectUfYsglById(tijianAccept.getDoctorId());
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setXsId(tijianAccept.getStudentId());
        ufTjglSsrw.setZirwid(tijianAccept.getZirwid());
        ufTjglSsrw.setSzxxid(tijianAccept.getSchoolId());
        UfTjglSsrw ufTjglSsrw1 = ufTjglSsrwService.selectUfTjglSsrwInfo(ufTjglSsrw);
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNull(ufTjglSsrw1)) {
            return AjaxResult.error("未查找到该学生体检信息！");
        } else {
            map.put("doctor", ufYsgl);
            map.put("studentInfo", ufTjglSsrw1);
            /*********  内科 ************************/
            Map<String, String> map_nk = new HashMap();
            if (StringUtils.isNotNull(ufTjglSsrw1.getxJg()) && (ufTjglSsrw1.getxJg().equals("9999"))) {
                map_nk.put("xjg", "心");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getgJg()) && (ufTjglSsrw1.getgJg().equals("9999"))) {
                map_nk.put("gjg", "肝");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getFhl()) && (ufTjglSsrw1.getFhl().toString().equals("9999"))) {
                map_nk.put("fhl", "肺活量");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getSsy()) && (ufTjglSsrw1.getSsy().toString().equals("9999"))) {
                map_nk.put("ssy", "收缩压");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getSzy()) && (ufTjglSsrw1.getSzy().toString().equals("9999"))) {
                map_nk.put("szy", "舒张压");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getfJg()) && (ufTjglSsrw1.getfJg().equals("9999"))) {
                map_nk.put("fjg", "肺");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getFbJg()) && (ufTjglSsrw1.getFbJg().equals("9999"))) {
                map_nk.put("fbjg", "腹部");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getpJg()) && (ufTjglSsrw1.getpJg().equals("9999"))) {
                map_nk.put("pjg", "脾");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getJbs()) && (ufTjglSsrw1.getJbs().equals("9999"))) {
                map_nk.put("jbs", "病史");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getNkqtJg()) && (ufTjglSsrw1.getNkqtJg().equals("9999"))) {
                map_nk.put("nkqt", "内科其他");
            }
            String nkValues = "";
            if (StringUtils.isNotNull(map_nk)) {
                nkValues = map_nk.values().stream()
                        .collect(Collectors.joining(", "));
            }
            map.put("nk", nkValues);

            /*********  外科 ************************/
            Map<String, String> map_wk = new HashMap();
            if (StringUtils.isNotNull(ufTjglSsrw1.getTbjJg()) && (ufTjglSsrw1.getTbjJg().equals("9999"))) {
                map_wk.put("tb", "头部");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getJbJg()) && (ufTjglSsrw1.getJbJg().equals("9999"))) {
                map_wk.put("jb", "颈部");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getXbJg()) && (ufTjglSsrw1.getXbJg().equals("9999"))) {
                map_wk.put("xb", "胸部");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getJzJg()) && (ufTjglSsrw1.getJzJg().equals("9999"))) {
                map_wk.put("jz", "脊柱");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getSzJg()) && (ufTjglSsrw1.getSzJg().equals("9999"))) {
                map_wk.put("sz", "四肢");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getLbjJg()) && (ufTjglSsrw1.getLbjJg().equals("9999"))) {
                map_wk.put("lbj", "淋巴结");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getPfJg()) && (ufTjglSsrw1.getPfJg().equals("9999"))) {
                map_wk.put("pf", "皮肤");
            }
            String wkValues = "";
            if (StringUtils.isNotNull(map_wk)) {
                wkValues = map_wk.values().stream()
                        .collect(Collectors.joining(", "));
            }
            map.put("wk", wkValues);

            /*************** 形态指标 ******************************/
            Map<String, String> map_xtzb = new HashMap<>();
            if (StringUtils.isNotNull(ufTjglSsrw1.getSg()) && (ufTjglSsrw1.getSg().compareTo(new BigDecimal(9999.0)) == 0)) {
                map_xtzb.put("sg", "身高");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getTz()) && (ufTjglSsrw1.getTz().compareTo(new BigDecimal(9999.0)) == 0)) {
                map_xtzb.put("tz", "体重");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getTw()) && (ufTjglSsrw1.getTw().compareTo(new BigDecimal(9999.0)) == 0)) {
                map_xtzb.put("tw", "臀围");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getYw()) && (ufTjglSsrw1.getYw().compareTo(new BigDecimal(9999.0)) == 0)) {
                map_xtzb.put("yw", "腰围");
            }
            String xtzbValues = "";
            if (StringUtils.isNotNull(map_xtzb)) {
                xtzbValues = map_xtzb.values().stream()
                        .collect(Collectors.joining(", "));
            }
            map.put("xtzb", xtzbValues);

            /**********  眼科  ***********/
            Map<String, String> map_yk = new HashMap();
            if (StringUtils.isNotNull(ufTjglSsrw1.getZy()) && (ufTjglSsrw1.getZy() == new BigDecimal(9999))) {
                map_yk.put("zy", "裸眼视力");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getYy()) && (ufTjglSsrw1.getYy() == new BigDecimal(9999))) {
                map_yk.put("zy", "裸眼视力");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getWgqt()) && (ufTjglSsrw1.getWgqt().equals("9999"))) {
                map_yk.put("wuqt", "五官其他");
            }
            String ykValues = "";
            if (StringUtils.isNotNull(map_yk)) {
                ykValues = map_yk.values().stream()
                        .collect(Collectors.joining(", "));
            }
            map.put("yk", ykValues);

            /*********************** 口腔科 *****/
            Map<String, String> map_kqk = new HashMap();
            if (StringUtils.isNotNull(ufTjglSsrw1.getYz()) && (ufTjglSsrw1.getYz() == 9999)) {
                map_kqk.put("yz", "牙周");
            }
            if (StringUtils.isNotNull(ufTjglSsrw1.getQc()) && (ufTjglSsrw1.getQc() == 9999)) {
                map_kqk.put("qc", "龋齿");
            }
            String qcValues = "";
            if (StringUtils.isNotNull(map_kqk)) {
                qcValues = map_kqk.values().stream()
                        .collect(Collectors.joining(", "));
            }
            map.put("kqk", qcValues);
            return AjaxResult.success(map);
        }
    }

    /**
     * 查询待检人数和已检查人数
     *
     * @param tijianAccept
     * @return
     */
    @PostMapping("/selectWaitNum")
    public AjaxResult selectWaitNum(@RequestBody tijianAccept tijianAccept) {
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setSzxxid(tijianAccept.getSchoolId());
        ufTjglSsrw.setZirwid(tijianAccept.getZirwid());
        //查询待检人数
        UfTjglSsrw ufTjglSsrw1 = ufTjglSsrwService.selectWaitNum(ufTjglSsrw);
        //查询已确认人数
        ufTjglSsrw.setZt(2L);
        UfTjglSsrw ufTjglSsrw2 = ufTjglSsrwService.selectWaitNum(ufTjglSsrw);
        //查询已检查人数
        UfSsrwLog UfSsrwLog = new UfSsrwLog();
        UfSsrwLog.setYsId(tijianAccept.getDoctorId());
        UfSsrwLog.setXxId(tijianAccept.getSchoolId());
        UfSsrwLog.setRwId(tijianAccept.getZirwid());
        UfSsrwLog ufSsrwLog1 = ufSsrwLogService.selectUfSsrwLogNum(UfSsrwLog);
        Map map = new HashMap();
        map.put("waitNum", ufTjglSsrw1.getRequestId());
        map.put("checkedNum", ufSsrwLog1.getRequestId());
        map.put("sureNum", ufTjglSsrw2.getRequestId());
        return AjaxResult.success(map);
    }

    /**
     * 确认接口
     */
    @GetMapping(value = "/sureTjrw/{id}")
    public AjaxResult sureTjrw(@PathVariable("id") Long id) {
        UfTjglSsrw tjglSsrw = new UfTjglSsrw();
        tjglSsrw.setId(id);
        tjglSsrw.setZt(2L);
        return toAjax(ufTjglSsrwService.updateUfTjglSsrw(tjglSsrw));
    }

    /**
     * 通过二维码获取学生体检信息 测试接口
     */
    @PostMapping("/getStudentQRcodeInfo1")
    public AjaxResult getStudentQRcodeInfo1() {
        UfTjglSsrw ufTjglSsrw = new UfTjglSsrw();
        ufTjglSsrw.setXsId(41L);
        ufTjglSsrw.setZirwid(12l);
        UfTjglSsrw ufTjglSsrw1 = ufTjglSsrwService.selectUfTjglSsrwInfo(ufTjglSsrw);
        Map<String, String> map = findFieldWithValue(ufTjglSsrw1, 9999);
        return AjaxResult.success(map);
    }

    public static Map<String, String> findFieldWithValue(Object obj, int targetValue) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Map<String, String> map = new HashMap<>();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                if (StringUtils.isNotNull(value) && (value.toString().equals("9999") || value.toString().equals("9999.0"))) {
                    ChineseDescription annotation = field.getAnnotation(ChineseDescription.class);
                    String chineseComment = "";
                    if (annotation != null) {
                        chineseComment = annotation.value();
                    }
                    map.put(field.getName(), chineseComment);
                    System.err.println(field.getName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static Map<String, String> findFieldWithValue1(Object obj, int targetValue) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Map<String, String> map = new HashMap<>();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                if (StringUtils.isNotNull(value) && (!value.toString().equals("0"))) {
                    ChineseDescription annotation = field.getAnnotation(ChineseDescription.class);
                    String chineseComment = "";
                    if (annotation != null) {
                        chineseComment = annotation.value();
                    }
                    map.put(field.getName(), chineseComment);
                    System.err.println("不等于0:" + field.getName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }


    /**
     * 家长  查询体检结果列表
     */
    @GetMapping("/jzdTiJianlist")
    public TableDataInfo jzdTiJianlist(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.jzdTiJianlist(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 平均身高男
     */
    @GetMapping("/selectAvgSg")
    public TableDataInfo selectAvgSg(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectAvgSg(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 平均体重
     */
    @GetMapping("/selectAvgTz")
    public TableDataInfo selectAvgTz(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectAvgTz(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 平均肺活量
     */
    @GetMapping("/selectAvgFhl")
    public TableDataInfo selectAvgFhl(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectAvgFhl(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 脊柱异常
     */
    @GetMapping("/selectJzYc")
    public TableDataInfo selectJzYc(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectJzYc(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 龋齿异常
     */
    @GetMapping("/selectQcYc")
    public TableDataInfo selectQcYc(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectQcYc(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检结果 异常占比
     */
    @GetMapping("/selectAbnormal")
    public AjaxResult selectInnutrition(UfTjglSsrw ufTjglSsrw) {
        Map<String, Object> map = new HashMap<>();
        //营养不良
        Map<String, Object> map_yy = ufTjglSsrwService.selectInnutrition(ufTjglSsrw);
        if (StringUtils.isNull(map_yy)) {
            return AjaxResult.success(map);
        }
        Long yysum = 0l;
        if (map_yy.size() > 0) {
            for (Object value : map_yy.values()) {
                if (value instanceof BigDecimal) {
                    yysum +=  ((BigDecimal) value).intValue();
                }
            }
        }

        map.put("yybl", yysum);
        //查询肥胖
        Map<String, Object> map_fp = ufTjglSsrwService.selectFeiPang(ufTjglSsrw);
        Long fpsum = 0l;
        if (map_yy.size() > 0) {
            for (Object value : map_fp.values()) {
                if (value instanceof BigDecimal) {
                    fpsum +=  ((BigDecimal) value).intValue();
                }
            }
        }
        map.put("sumFp", fpsum);

        BigDecimal slsum = new BigDecimal(BigInteger.ZERO);
        //查询视力不良
        List<UfTjglSsrw> slList = ufTjglSsrwService.selectSlYc(ufTjglSsrw);
        if (slList.size() > 0) {
            for (int i = 0; i < slList.size(); i++) {
                slsum = slsum.add(slList.get(i).getTz().add(slList.get(i).getSg()));
            }
        }
        //查询脊柱异常
        BigDecimal JZsum = new BigDecimal(BigInteger.ZERO);
        List<UfTjglSsrw> jzList = ufTjglSsrwService.selectJzYc(ufTjglSsrw);
        if (jzList.size() > 0) {
            for (int i = 0; i < jzList.size(); i++) {
                JZsum = JZsum.add(jzList.get(i).getTz().add(jzList.get(i).getSg()));
            }
        }
        UfTjglSsrw ssrw = ufTjglSsrwService.selectZongNum(ufTjglSsrw);
        map.put("sumAll", ssrw.getId());
        map.put("sumSl", slsum);
        map.put("sumJz", JZsum);
        return AjaxResult.success(map);
    }

    /**
     * 查询营养状态
     */
    @GetMapping("/selectNutritional")
    public AjaxResult selectNutritional(UfTjglSsrw ufTjglSsrw) {
        Map<String, Object> map = new HashMap<>();
        //正常
        Map<String, Object> map_ZC = ufTjglSsrwService.selectZcBmi(ufTjglSsrw);
        Long zcsum = 0l;
        Long czsum = 0l;
        if (StringUtils.isNull(map_ZC)) {
            map.put("sumZc", zcsum);
            map.put("sumCz", czsum);
            map.put("yySumAll", 0);
            return AjaxResult.success(map);

        }
        if (map_ZC.size() > 0) {
            for (Object value : map_ZC.values()) {
                if (value instanceof Integer) {
                    zcsum += (Integer) value;
                }
            }
        }
        map.put("sumZc", zcsum);
        //超重
        Map<String, Object> map_CZ = ufTjglSsrwService.selectCzBmi(ufTjglSsrw);

        if (map_CZ.size() > 0) {
            for (Object value : map_CZ.values()) {
                if (value instanceof Integer) {
                    czsum += (Integer) value;
                }
            }
        }
        map.put("sumCz", czsum);
        //查询总数
        ufTjglSsrw.setBmi(new BigDecimal("1"));
        UfTjglSsrw ssrw = ufTjglSsrwService.selectZongNum(ufTjglSsrw);
        map.put("yySumAll", ssrw.getId());
        return AjaxResult.success(map);
    }


    /**
     * 查询体检结果 视力不良
     */
    @GetMapping("/selectSlYc")
    public TableDataInfo selectSlYc(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectSlYc(ufTjglSsrw);
        return getDataTable(list);
    }


    /**
     * 查询上次体检结果
     */
    @GetMapping("/selectLastSsrw")
    public TableDataInfo selectLastSsrw(UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectLastSsrw(ufTjglSsrw);
        return getDataTable(list);
    }

    /**
     * 查询体检概览
     */
    @GetMapping("/selectTjOverview")
    public AjaxResult selectTjOverview(UfTjglSsrw tjglSsrw) {
        if (StringUtils.isNotNull(tjglSsrw.getSzxxid())) {
            UfZzjg ufZzjg = ufZzjgService.selectUfZzjgById(tjglSsrw.getSzxxid());
            //1市集 2区级
            if (ufZzjg.getLx() != 6 && ufZzjg.getLx() != 3) {
                UfZzjg ufZzjg1 = new UfZzjg();
                ufZzjg1.setLx(6L);
                ufZzjg1.setId(tjglSsrw.getSzxxid());
                List<UfZzjg> list1 = ufZzjgService.selectChildDept(ufZzjg1);
                String ids[] = new String[list1.size()];
                for (int i = 0; i < list1.size(); i++) {
                    ids[i] = String.valueOf(list1.get(i).getId());
                }
                tjglSsrw.setSzxxid(null);
                tjglSsrw.setSzxxids(ids);
            } else if (ufZzjg.getLx() == 6) {
                tjglSsrw.setSzbjid(tjglSsrw.getSzxxid());
                tjglSsrw.setSzxxid(null);
            }
        }
        if (StringUtils.isNotNull(tjglSsrw.getRoleType()) ) {
            if (tjglSsrw.getRoleType().equals("JZG")) {
                tjglSsrw.setZhurwid(tjglSsrw.getZirwid());
                tjglSsrw.setZirwid(null);
            }
        }
        //查询总合
        UfTjglSsrw ob = ufTjglSsrwService.selectNum(tjglSsrw);
        Long zong = ob.getId();
        //查询正常
        tjglSsrw.setTjjg(0L);
        tjglSsrw.setZt(2L);
        UfTjglSsrw ob_z = ufTjglSsrwService.selectNum(tjglSsrw);
        Long zc = ob_z.getId();
        //查询不正常
        tjglSsrw.setTjjg(1L);
        UfTjglSsrw ob_b = ufTjglSsrwService.selectNum(tjglSsrw);
        Long yc = ob_b.getId();
        //查询缺检查
        tjglSsrw.setTjjg(null);
        tjglSsrw.setZt(3L);
        UfTjglSsrw ob_q = ufTjglSsrwService.selectNum(tjglSsrw);
        Long qj = ob_q.getId();
        //查询已检查人数
        Long yj = zong - qj;
        //查询正常
        tjglSsrw.setTjjg(null);
        tjglSsrw.setZt(2L);
        UfTjglSsrw ob_qr = ufTjglSsrwService.selectNum(tjglSsrw);
        Long qr = ob_qr.getId();
        Map<String, Long> map = new HashMap<>();
        map.put("allNum", zong);
        map.put("zc", zc);
        map.put("yc", yc);
        map.put("qj", qj);
        map.put("yj", yj);
        map.put("qr", qr);
        return AjaxResult.success(map);
    }


    /**
     * 体检结果总和
     */
    @PostMapping("/selectTjjgNum")
    public AjaxResult tjjgNum(@RequestBody UfTjglSsrw tjglSsrw) {

        if (StringUtils.isNotNull(tjglSsrw.getSzxxid())) {
            UfZzjg ufZzjg = ufZzjgService.selectUfZzjgById(tjglSsrw.getSzxxid());
            //1市集 2区级
            if (ufZzjg.getLx() != 6 && ufZzjg.getLx() != 3) {
                UfZzjg ufZzjg1 = new UfZzjg();
                ufZzjg1.setLx(6L);
                ufZzjg1.setId(tjglSsrw.getSzxxid());
                List<UfZzjg> list1 = ufZzjgService.selectChildDept(ufZzjg1);
                String ids[] = new String[list1.size()];
                for (int i = 0; i < list1.size(); i++) {
                    ids[i] = String.valueOf(list1.get(i).getId());
                }
                tjglSsrw.setSzxxid(null);
                tjglSsrw.setSzxxids(ids);
            } else if (ufZzjg.getLx() == 6) {
                tjglSsrw.setSzbjid(tjglSsrw.getSzxxid());
                tjglSsrw.setSzxxid(null);
            }
        }
        if (StringUtils.isNotNull(tjglSsrw.getRoleType()) ) {
            if (tjglSsrw.getRoleType().equals("JZG")) {
                tjglSsrw.setZhurwid(tjglSsrw.getZirwid());
                tjglSsrw.setZirwid(null);
            }
        }
        //查询总合
        UfTjglSsrw ob = ufTjglSsrwService.selectNum(tjglSsrw);
        Long zong = ob.getId();
        //查询正常
        tjglSsrw.setTjjg(0L);
        UfTjglSsrw ob_z = ufTjglSsrwService.selectNum(tjglSsrw);
        Long zc = ob_z.getId();
        //查询不正常
        tjglSsrw.setTjjg(1L);
        UfTjglSsrw ob_b = ufTjglSsrwService.selectNum(tjglSsrw);
        Long yc = ob_b.getId();
        //查询未完成
        tjglSsrw.setTjjg(2L);
        UfTjglSsrw ob_w = ufTjglSsrwService.selectNum(tjglSsrw);
        Long wwc = ob_w.getId();
        Map<String, Long> map = new HashMap<>();
        map.put("allNum", zong);
        map.put("zc", zc);
        map.put("yc", yc);
        map.put("wwc", wwc);
        return AjaxResult.success(map);
    }


    /**
     * 导出体检结果列表
     */
    @Log(title = "体检结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfTjglSsrw ufTjglSsrw) {
        List<UfTjglSsrw> list = ufTjglSsrwService.selectUfTjglSsrwList(ufTjglSsrw);
        ExcelUtil<UfTjglSsrw> util = new ExcelUtil<UfTjglSsrw>(UfTjglSsrw.class);
        util.exportExcel(response, list, "体检结果数据");
    }

    /**
     * 获取体检结果详细信息
     */
    @GetMapping(value = "/selectTjResult/{id}")
    public AjaxResult selectTjResult(@PathVariable("id") Long id) {
        return success(ufTjglSsrwService.selectUfTjglSsrwById(id));
    }

    /**
     * 新增体检结果
     */
    @Log(title = "体检结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UfTjglSsrw ufTjglSsrw) {
        return toAjax(ufTjglSsrwService.insertUfTjglSsrw(ufTjglSsrw));
    }

    /**
     * 修改体检结果
     */
    @Log(title = "体检结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UfTjglSsrw ufTjglSsrw) {
        return toAjax(ufTjglSsrwService.updateUfTjglSsrw(ufTjglSsrw));
    }

    /**
     * 删除体检结果
     */
    @Log(title = "体检结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ufTjglSsrwService.deleteUfTjglSsrwByIds(ids));
    }
}
